//
//  MainVistorView.swift
//  Weibo
//
//  Created by mac on 16/1/11.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit
import SnapKit

@objc protocol MainVistorViewDelegate: NSObjectProtocol {
    
    //MARK: 代理方法 用户点击登录按钮
    func userWillLogin()
    
    //MARK: 代理方法  用户点击注册按钮
    optional func userWillRegister()
    
}

class MainVistorView: UIView {
    
   weak var delegate: MainVistorViewDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
       
        addSubviews()
        autoLayoutSubViews()
        
        rorationStarting()
        
    }
    

    //MARK: 外部接口改变视图中的内容
    func changeViewBy(imageName:String, tipText:String) {
        if let img:String = imageName {
            rotationImageView.image = UIImage(named: img)
            bigHourse.hidden = true
            bringSubviewToFront(rotationImageView)
            self.tipLabel.text = tipText
            rotationImageView.layer.removeAnimationForKey("rotation")
        }
        
    }
    
    //MARK: 添加控件
    private func addSubviews(){
        addSubview(rotationImageView)
        addSubview(coverImageView)
        addSubview(bigHourse)
        addSubview(tipLabel)
        addSubview(loginBtn)
        addSubview(registerBtn)
        
    }
    
    //MARK: 自动布局
    private func autoLayoutSubViews(){
        rotationImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-80)
        }
        
        bigHourse.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(rotationImageView.snp_center)
        }
        
        tipLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(bigHourse.snp_bottom).offset(60)
            make.width.equalTo(225)
            make.height.equalTo(40)
            make.centerX.equalTo(bigHourse.snp_centerX)
        }
        
        loginBtn.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(tipLabel.snp_bottom).offset(20)
            make.left.equalTo(tipLabel.snp_left)
            make.width.equalTo(100)
        }
        
        registerBtn.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(tipLabel.snp_bottom).offset(20)
            make.right.equalTo(tipLabel.snp_right)
            make.width.equalTo(100)
        }
        
        coverImageView.snp_makeConstraints { (make) -> Void in
            make.top.left.right.equalTo(self)
            make.bottom.equalTo(registerBtn.snp_bottom)
        }
        self.backgroundColor = UIColor(white: 0.93, alpha: 1)
        
        self.loginBtn.addTarget(self, action: "userLogin", forControlEvents: .TouchUpInside)
        
        self.registerBtn.addTarget(self, action: "userRegister", forControlEvents: .TouchUpInside)
    }
    
    //MARK: 圈圈转动
    private func rorationStarting() {
        let basicAnimation: CABasicAnimation = CABasicAnimation(keyPath: "transform.rotation")
    
        basicAnimation.repeatCount = MAXFLOAT
        basicAnimation.duration = 12
        basicAnimation.toValue = 2 * M_PI
        basicAnimation.removedOnCompletion = false
        
        rotationImageView.layer.addAnimation(basicAnimation, forKey: "rotation")
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    //MARK: 圆圈
    lazy var rotationImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    //MARK: 大房子
    lazy var bigHourse: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    //MARK: 遮罩
    lazy var coverImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    //MARK: 提示文字
    lazy var tipLabel: UILabel = UILabel(title: "登录后,可以看到别人评论你的微博", color: UIColor.grayColor(), fontSize: 15)
    
    //MARK: 登录按钮
    lazy var loginBtn: UIButton = UIButton(title: "登  录", color: UIColor.grayColor(), fontSize: 16)
    
    //MARK: 注册按钮
    lazy var registerBtn: UIButton = UIButton(title: "注  册", color: UIColor.orangeColor(), fontSize: 16)
    
    @objc private func userLogin() {

        self.delegate?.userWillLogin()
    }
    
    
    @objc private func userRegister() {
        self.delegate?.userWillRegister?()
    }

}














